# coding=utf-8
# 代码文件：chapter10/ch10.1.3.py
import os

from PIL import Image

# 设置输入目录
indir = r'images\in'
#  设置输出目录
outdir = r'images\out'

if __name__ == '__main__':

    # 查找indir目录中所有文件
    allfile = os.listdir(indir)

    for name in allfile:
        infile = os.path.join(indir, name)
        outfile = os.path.join(outdir, name)

        try:
            # 打开图片文件
            im = Image.open(infile)
            #  重新设置图片大小
            # w = 500             # 指定图片宽度
            # h = 500             # 指定图片高度
            factor = 0.5        # 指定缩放因子

            w = round(im.size[0] * factor)
            h = round(im.size[1] * factor)

            resized_im = im.resize((w, h))
            # 保存图片
            resized_im.save(outfile)

        except IOError as e:
            print(e)
            # 继续转换下一个文件
            continue

    print('转换完成！')
